<?php

/**
 * A resultset for the PDO database adaptor.
 * @package Nina
 * @author John Main <john@thedaddy.org>, Chris Corbyn
 */
class Nina_Db_Driver_Result_Pdo implements Nina_Db_Driver_Result
{
	/**
	 * The PDO instance.
	 * @var PDO
	 */
	protected $pdo = null;
	/**
	 * The executed statement instance.
	 * @var PDOStatement
	 */
	protected $pdoResult = null;
	/**
	 * The row offset.
	 * @var int
	 */
	protected $rowPos = 0;

	public function __construct($pdo) {
		$this->pdo = $pdo;
	}
	
	/**
	 * Set the PDO instance.
	 * @param PDO
	 */
	public function setPdo(PDO $pdo) {
		$this->pdo = $pdo;
	}
	
	/**
	 * Get the PDO instance.
	 * @return PDO
	 */
	public function getPdo() {
		return $this->pdo;
	}
	
	/**
	 * Set the PDO statement/result.
	 * @param PDOStatement $rs
	 */
	public function setPdoResult(PDOStatement $rs) {
		$this->pdoResult = $rs;
	}
	
	/**
	 * Get the PDO statement/result.
	 * @return PDOStatement
	 */
	public function getPdoResult() {
		return $this->pdoResult;
	}
	
	/**
	 * Get the number of affected rows.
	 * @return int
	 */
	public function affected() {
		$ret = $this->getPdoResult()->rowCount();
		return $ret;
	}
	
	/**
	 * Fetch a single row and scroll through the resultset.
	 * @return array
	 */
	public function row() {
		return $this->getPdoResult()->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT, $this->rowPos++);
	}
	
	/**
	 * Return the number of rows in the resultset.
	 * @return array
	 */
	public function count() {
		return $this->getPdoResult()->rowCount();
	}
	
	/**
	 * Seek to the given position in the resultset.
	 * @param integer $pos Position
	 */
	public function seek($pos) {
		$this->rowPos = (int)$pos;
	}
}
